const pathResolver = require('../config/path_resolver');
const pageHelper = require('../../config/path_import.js').pageHelper;
const ProjectBiz = require('../../../comm/biz/project_biz.js');

Page({
  data: {
    expression: '',
    result: '0',
    lastIsOperator: false
  },

  onLoad: function (options) {
    ProjectBiz.initPage(this);
  },

  // 添加数字
  appendNumber: function (e) {
    const num = e.currentTarget.dataset.num;
    let expression = this.data.expression;
    
    // 如果表达式为空且点击的是0，不做任何操作
    if (expression === '' && num === '0') return;
    
    this.setData({
      expression: expression + num,
      lastIsOperator: false
    });
    
    this.calculateResult();
  },

  // 添加小数点
  appendDecimal: function () {
    let expression = this.data.expression;
    
    // 如果表达式为空，先添加0
    if (expression === '') {
      expression = '0';
    }
    
    // 检查最后一个数字是否已经有小数点
    const parts = expression.split(/[\+\-\*\/\%]/);
    const lastPart = parts[parts.length - 1];
    
    if (lastPart.includes('.')) return;
    
    this.setData({
      expression: expression + '.',
      lastIsOperator: false
    });
  },

  // 添加运算符
  appendOperator: function (e) {
    const op = e.currentTarget.dataset.op;
    let expression = this.data.expression;
    
    // 如果表达式为空且不是减号，不做任何操作
    if (expression === '' && op !== '-') return;
    
    // 如果表达式为空且是减号，直接添加
    if (expression === '' && op === '-') {
      this.setData({
        expression: '-',
        lastIsOperator: true
      });
      return;
    }
    
    // 如果上一个字符是运算符，替换它
    if (this.data.lastIsOperator) {
      expression = expression.slice(0, -1) + op;
    } else {
      expression = expression + op;
    }
    
    this.setData({
      expression: expression,
      lastIsOperator: true
    });
  },

  // 清除
  clear: function () {
    this.setData({
      expression: '',
      result: '0',
      lastIsOperator: false
    });
  },

  // 退格
  backspace: function () {
    let expression = this.data.expression;
    
    if (expression.length > 0) {
      // 检查删除的是否是运算符
      const lastChar = expression.slice(-1);
      const isOperator = /[\+\-\*\/\%]/.test(lastChar);
      
      expression = expression.slice(0, -1);
      
      this.setData({
        expression: expression,
        lastIsOperator: expression.length > 0 ? /[\+\-\*\/\%]/.test(expression.slice(-1)) : false
      });
      
      if (expression === '') {
        this.setData({ result: '0' });
      } else {
        this.calculateResult();
      }
    }
  },

  // 计算结果
  calculate: function () {
    if (this.data.expression === '') return;
    
    try {
      // 替换显示符号为实际运算符
      let expression = this.data.expression.replace(/×/g, '*').replace(/÷/g, '/');
      
      // 计算结果
      let result = eval(expression);
      
      // 处理小数点后过多的位数
      if (result.toString().includes('.')) {
        result = parseFloat(result.toFixed(8));
      }
      
      this.setData({
        expression: result.toString(),
        result: result.toString(),
        lastIsOperator: false
      });
    } catch (e) {
      this.setData({
        result: '错误'
      });
    }
  },

  // 实时计算结果
  calculateResult: function () {
    if (this.data.expression === '') {
      this.setData({ result: '0' });
      return;
    }
    
    try {
      // 替换显示符号为实际运算符
      let expression = this.data.expression.replace(/×/g, '*').replace(/÷/g, '/');
      
      // 如果最后一个字符是运算符，去掉它再计算
      if (this.data.lastIsOperator) {
        expression = expression.slice(0, -1);
      }
      
      // 计算结果
      let result = eval(expression);
      
      // 处理小数点后过多的位数
      if (result.toString().includes('.')) {
        result = parseFloat(result.toFixed(8));
      }
      
      this.setData({
        result: result.toString()
      });
    } catch (e) {
      // 表达式不完整或有错误，不更新结果
    }
  },

  url: function (e) {
    pageHelper.url(e, this);
  }
}); 